<?php

class AdminRolesController extends Base {
  use CURD;

  public $tableName = 'admin_roles';
  protected $likeField = 'name';
  protected $deleted_at = false;
  protected $isTree = false;


  private function getMenusKey($roleId){
  	$menuList = $this->db->select('admin_role_menu','*',['role_id'=>$roleId]);
  	$menuKeys = [];
  	$menuIds = array_column($menuList, 'menu_id');
  	if($menuIds || intval($roleId) === 1){
      $where = ['id'=>$menuIds];
      if(intval($roleId) === 1){
        $where = [];
      }
  		$tmp = $this->db->select('admin_menu',['key'],$where);
  		$menuKeys = array_column($tmp,'key');
  	}
  	return  $menuKeys;
  }

  private function getPermsKey($roleId){
  	$keys = [];
  	$auths = [];
  	$pers = $this->db->select('admin_role_permissions','*',['role_id'=>$roleId]);
  	$ids = array_column($pers, 'permission_id');
  	if($ids || intval($roleId) === 1){
      $where = ['id'=>$ids];
      if(intval($roleId) === 1){
        $where = [];
      }
  		$list = $this->db->select('admin_permissions','*',$where);  		  	
  		foreach ($list as $value) {
  			$keys[] = $value['parent_id'].'.'.$value['slug'].':'.$value['id'];
  			$auths[] = $value['parent_id'].'.'.$value['slug'];
  		}
  	}
  	return  compact('keys','auths');
  }
  

  public function menusAction(){
  	$roleId = $this->getUser()['role_id'];    
  	$menuList = $this->db->select('admin_role_menu','*',['role_id'=>$roleId]);
  	$menus = [];
  	$menuIds = array_column($menuList, 'menu_id');
  	if($menuIds || intval($roleId) === 1){
      $where = ['id'=>$menuIds,'show'=>1];
      if(intval($roleId) === 1){
        $where = ['show'=>1];
      }
      $where['ORDER'] = ['order'=>'ASC','id'=>'ASC'];
  		$list = $this->db->select('admin_menu','*',$where);    
  		$menus = getTreeChildren($list,'parent_id');
  	}
  	success(compact('menus'));
  }


  public function infoAction(){
    $user = $this->getUser();
    $roleId = $user['role_id']; 
    $auths = $this->getPermsKey($roleId)['auths'];
    success(compact('user','auths')); 
  }


  public function permissionAction(){
    $roleId = $this->getRequest()->getPost('roleId');
    if(!$roleId){
      $roleId = $this->getUser()['role_id'];
    }
    $a = $this->getMenusKey($roleId);
    $b =  $this->getPermsKey($roleId);
    $result = array_merge($a,$b['keys']);
    success($result);
  }


  public function permissionsAction(){
  	$post = $this->getRequest()->getPost();
  	$roleId = $post['roleId'];
  	$menus = $post['menus'];
  	$this->db->delete('admin_role_menu',['role_id'=>$roleId]);
  	$this->db->delete('admin_role_permissions',['role_id'=>$roleId]);
  	$time = date('Y-m-d H:i:s');
  	if(is_array($menus) && count($menus)){
  		$menuList = $this->db->select('admin_menu',['id'],['key'=>$menus]);
  		$inserts = [];
  		foreach ($menuList as $value) {
  			$inserts[] = [
  				'role_id'=>$roleId,
  				'menu_id'=>$value['id'],
  				'created_at' => $time
  			];
  		}
  		if($inserts){
  			$this->db->insert('admin_role_menu',$inserts);
  		}  

  		$inserts = [];
  		foreach ($menus as $item) {
  			if(strpos($item,':') > 0){
  				$permission_id = explode(':',$item)[1];
  				$inserts[] = [
	  				'role_id'=>$roleId,
	  				'permission_id'=>$permission_id,
	  				'created_at' => $time
	  			];
  			}
  		}
  		if($inserts){
  			$this->db->insert('admin_role_permissions',$inserts);
  		} 

  	}
  	
  	success($inserts);
  }

}
